<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生学号" prop="stuNumber">
        <el-input
            v-model="queryParams.stuNumber"
            placeholder="请输入学生学号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="stuName">
        <el-input
            v-model="queryParams.stuName"
            placeholder="请输入学生姓名"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生性别" prop="stuSex">
        <el-select v-model="queryParams.stuSex" placeholder="请选择学生性别" clearable>
          <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="班级名称" prop="collegeId">
        <el-select v-model="queryParams.stuClassId" placeholder="请选择班级" clearable>
          <el-option
              v-for="item in fromClass"
              :key="item.classId"
              :label="item.className"
              :value="item.classId"/>
        </el-select>
      </el-form-item>
        <el-form-item label="出生年月" prop="stuBirth">
        <el-date-picker clearable
                        v-model="queryParams.stuBirth"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出生年月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="居住地址" prop="stuSite">
        <el-input
            v-model="queryParams.stuSite"
            placeholder="请输入居住地址"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生电话" prop="stuPhone">
        <el-input
            v-model="queryParams.stuPhone"
            placeholder="请输入学生电话"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="专业名称" prop="professionalId">
        <el-select v-model="queryParams.stuProfessionalId" placeholder="请选择专业" clearable :style="{width: '100%'}">
          <el-option
              v-for="item in fromProfessional"
              :key="item.professionalId"
              :label="item.professionalName"
              :value="item.professionalId"/>
        </el-select>
      </el-form-item>

      <el-form-item label="备注信息" prop="stuMemo">
        <el-input
            v-model="queryParams.stuMemo"
            placeholder="请输入备注信息"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除标识" prop="deleteSign">
        <el-input
            v-model="queryParams.deleteSign"
            placeholder="请输入删除标识"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['teaching:stu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['teaching:stu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['teaching:stu:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['teaching:stu:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生序号" align="center" prop="stuId" />
      <el-table-column label="学生学号" align="center" prop="stuNumber" />
      <el-table-column label="学生姓名" align="center" prop="stuName" />
      <el-table-column label="头像" align="center" prop="stuPhotoUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.stuPhotoUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="学生性别" align="center" prop="stuSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.stuSex"/>
        </template>
      </el-table-column>
      <el-table-column label="出生年月" align="center" prop="stuBirth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stuBirth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="居住地址" align="center" prop="stuSite" />
      <el-table-column label="学生电话" align="center" prop="stuPhone" />
      <el-table-column label="所属班级" align="center" prop="className" />
      <el-table-column label="专业名称" align="center" prop="professionalName" />
      <el-table-column label="备注信息" align="center" prop="stuMemo" />
      <el-table-column label="删除标识" align="center" prop="deleteSign" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['teaching:stu:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['teaching:stu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生学号" prop="stuNumber">
          <el-input v-model="form.stuNumber" placeholder="请输入学生学号" />
        </el-form-item>
        <el-form-item label="学生姓名" prop="stuName">
          <el-input v-model="form.stuName" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="头像">
          <image-upload v-model="form.stuPhotoUrl"/>
        </el-form-item>
        <el-form-item label="学生性别" prop="stuSex">
          <el-select v-model="form.stuSex" placeholder="请选择学生性别">
            <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生年月" prop="stuBirth">
          <el-date-picker clearable
                          v-model="form.stuBirth"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出生年月">
          </el-date-picker>
        </el-form-item>

<!--        <el-form-item label="居住地址" prop="stuSite">-->
<!--          <el-input v-model="form.stuSite" placeholder="请输入居住地址" />-->
<!--        </el-form-item>-->

<!--        <el-form-item label="居住地址" prop="stuSite">-->
<!--          <el-cascader v-model="form.stuSite" :options="province" :props="provinceProps"-->
<!--                       :style="{width: '100%'}" placeholder="请选择居住地址" clearable></el-cascader>-->
<!--        </el-form-item>-->

<!--        <el-form-item label="居住地址" prop="stuSite">-->
<!--          <el-cascader v-model="province" :options="province" :props="provinceProps"-->
<!--                       :style="{width: '100%'}" placeholder="请选择居住地址" separator="" filterable clearable></el-cascader>-->
<!--        </el-form-item>-->
        <el-form-item label="居住地址">
          <el-cascader size="large" :options="options" v-model="form.stuSite" @change="handleChange"
                       placeholder="请选择省/市/区"
                       :style="{width: '100%'}" separator="/" filterable clearable></el-cascader>
        </el-form-item>

<!--        <el-form-item label="居住地址">-->
<!--          <el-cascader size="large" :options="options" v-model="selectedOptions" change-on-select></el-cascader>-->
<!--        </el-form-item>-->


        <el-form-item label="学生电话" prop="stuPhone">
          <el-input v-model="form.stuPhone" placeholder="请输入学生电话" />
        </el-form-item>

        <el-form-item label="班级选择" prop="collegeId">
          <el-select v-model="form.stuClassId" placeholder="请选择班级" clearable :style="{width: '100%'}">
            <el-option
                v-for="item in fromClass"
                :key="item.classId"
                :label="item.className"
                :value="item.classId"/>
          </el-select>
        </el-form-item>


        <el-form-item label="专业选择" prop="professionalId">
          <el-select v-model="form.stuProfessionalId" placeholder="请选择专业" clearable :style="{width: '100%'}">
            <el-option
                v-for="item in fromProfessional"
                :key="item.professionalId"
                :label="item.professionalName"
                :value="item.professionalId"/>
          </el-select>
        </el-form-item>

        <el-form-item label="备注信息" prop="stuMemo">
          <el-input v-model="form.stuMemo" placeholder="请输入备注信息" />
        </el-form-item>
        <el-form-item label="删除标识" prop="deleteSign">
          <el-input v-model="form.deleteSign" placeholder="请输入删除标识" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStu, getStu, delStu, addStu, updateStu } from "@/api/teaching/stu";
import { getProvince,getCity,getCounty,getTown,getVillage }from "@/api/teaching/position";
import { regionData, CodeToText } from "element-china-area-data"
import { getClassCode } from "@/api/teaching/class";
import { listProfessionalAll } from "@/api/teaching/professional";

export default {
  name: "Stu",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生信息表格数据
      stuList: [],
      //省市区三级联动
      options: regionData,
      fromProfessional: [],
      // 省
      province: [],
      fromClass:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuNumber: null,
        stuName: null,
        stuPhotoUrl: null,
        stuSex: null,
        stuBirth: null,
        stuSite: null,
        stuPhone: null,
        stuClassId: null,
        stuProfessionalId: null,
        stuMemo: null,
        deleteSign: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生信息列表 */
    async getList() {
      this.loading = true;
      await listStu(this.queryParams).then(response => {
        this.stuList = response.rows;
        this.total = response.total;
      });
      await getProvince().then(response => {
        this.province = response.data;
      });
      await getClassCode('1').then(response => {
        this.fromClass = response.data;
      });
      await listProfessionalAll().then(response => {
        this.fromProfessional = response.data;
      });
      for (var i = 0; i < this.stuList.length; i++) {
        for (var j = 0; j < this.fromClass.length; j++) {
          if(this.stuList[i].stuClassId == this.fromClass[j].classId){
            this.$set(this.stuList[i],"className",this.fromClass[j].className);
            break;

          }else {
            this.stuList[i].className = "错误";
          }
          }
        for (var j = 0; j < this.fromProfessional.length; j++) {
          if(this.stuList[i].stuProfessionalId == this.fromProfessional[j].professionalId){
            this.$set(this.stuList[i],"professionalName",this.fromProfessional[j].professionalName);
            break;
          }else {
            this.stuList[i].professionalName = "错误";
          }
        }

      }
      this.loading = false;
    },
    // getProvince() {
    //   // TODO 发起请求获取数据
    //   // this.province
    //   getProvince().then(response => {
    //     this.province = response.data;
    //     console.log(this.province)
    //   });
    // },
    handleChange(value){
      this.form.stuSite[0] = CodeToText[value[0]]
      this.form.stuSite[1] = CodeToText[value[1]]
      this.form.stuSite[2] = CodeToText[value[2]]
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stuId: null,
        stuNumber: null,
        stuName: null,
        stuPhotoUrl: null,
        stuSex: null,
        stuBirth: null,
        stuSite: null,
        stuPhone: null,
        stuClassId: null,
        stuProfessionalId: null,
        stuMemo: null,
        deleteSign: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stuId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stuId = row.stuId || this.ids
      getStu(stuId).then(response => {
        this.form = response.data;
        this.form.stuSite = this.form.stuSite.split("/");
        this.open = true;
        this.title = "修改学生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.stuSite = this.form.stuSite[0]+"/" +this.form.stuSite[1]+"/"+this.form.stuSite[2];
        if (valid) {
          if (this.form.stuId != null) {
            updateStu(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStu(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stuIds = row.stuId || this.ids;
      this.$modal.confirm('是否确认删除学生信息编号为"' + stuIds + '"的数据项？').then(function() {
        return delStu(stuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/stu/export', {
        ...this.queryParams
      }, `stu_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
